package com.mapbar.ure.frontend.lr1;

import com.mapbar.ure.utils.ChangingMonitorHashSet;

public class CanonicalCollection {
	private ChangingMonitorHashSet<LR1Item> set;
	private boolean changing = false;
	private boolean marked = false;

	public void mark() {
		this.marked = true;
	}

	public boolean isMarked() {
		return marked;
	}

	public CanonicalCollection() {
		this.set = new ChangingMonitorHashSet<LR1Item>();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof CanonicalCollection) {
			ChangingMonitorHashSet<LR1Item> cc0 = ((CanonicalCollection) obj)
					.getSet();
			if (set.containsAll(cc0) && cc0.containsAll(set)) {
				return true;
			}
			return false;
		} else {
			return false;
		}
	}

	/**
	 * TODO 这里待考虑是否需要重新实现
	 */
	@Override
	public int hashCode() {
		return super.hashCode();
	}

	public ChangingMonitorHashSet<LR1Item> getSet() {
		return set;
	}

	public boolean addItem(LR1Item item) {
		boolean added = set.add(item);
		if (added && !changing) {
			changing = true;
		}
		return added;
	}

	public boolean stillChanging() {
		if (!changing) {
			return changing;
		} else {
			boolean ret = changing;
			changing = false;
			return ret;
		}
	}
}
